package StudyCode.summary.regularexpressionsmatch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class FindAndReplaceUsingRegularExpressions {
	public static void main(String[] args) {
		String regex = "\\b\\d+\\b";
		StringBuffer sb = new StringBuffer();
		String replacementText = "";
		String matchedText = "";

		String text = "A test test 125 test test this is a test" +
				" the value is 100 test test. " +
				"This is a test.";

		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(text);

		while (m.find()) {
			matchedText = m.group();

			int num = Integer.parseInt(matchedText);

			if (num == 100) {
				replacementText = "a hundred";
			} else if (num < 100) {
				replacementText = "less than a hundred";
			} else {
				replacementText = "more than a hundred";
			}

			m.appendReplacement(sb, replacementText);
		}

		m.appendTail(sb);
		System.out.println("旧文本: " + text);
		System.out.println("新文本: " + sb.toString());
	}
}
